const blur_frag = /*wgsl*/ `
struct Input {  
    @location(0) uv : vec2<f32>,
};
struct Output {
    @location(0) color:vec4<f32>,
};
// 基于物理的散射强度计算
// fn calculateScattering(distance: f32, density: f32, wavelength: f32) -> f32 {
//     // Rayleigh散射公式改进
//     let scatterCoef = pow(wavelength, -4.0) * density;
//     // 米氏散射
//     let g = 0.76;  // 不对称因子
//     let theta = dot(lightDir, viewDir);
//     let mieScatter = (1.0 - g * g) / pow(1.0 + g * g - 2.0 * g * theta, 1.5);
//     return exp(-distance * scatterCoef) * mieScatter;
// }
fn calculateScattering(distance: f32, density: f32, wavelength: f32) -> f32 {
    // 简化的散射计算，只考虑距离和波长的影响
    let scatterCoef = pow(wavelength, -4.0) * density;
    // 使用简单的指数衰减
    let value = exp(-distance * scatterCoef) * (1.0 - distance);
    // 限制返回值在 [0, 1] 范围内
    return clamp(value, 0.0, 1.0);
}
// 首先需要定义结构体
struct WavelengthScattering {
    red: f32,
    green: f32,
    blue: f32,
}
fn calculateColorDispersion(distance: f32) -> vec3<f32> {
    var scatter: WavelengthScattering;
    scatter.red = calculateScattering(distance, density, 650.0);
    scatter.green = calculateScattering(distance, density, 550.0);
    scatter.blue = calculateScattering(distance, density, 450.0);
    return vec3<f32>(scatter.red, scatter.green, scatter.blue);
}
@group(0) @binding(0) var tDiffuse: texture_2d<f32>;
@group(0) @binding(1) var Sampler: sampler;
@group(0) @binding(2) var <uniform> density : f32;
@group(0) @binding(3) var <uniform> baseRadius : f32;  // 基础半径
@fragment
fn main(input:Input) -> Output {
    var output:Output;
    var texel = textureSample(tDiffuse, Sampler, input.uv);
    var luma = vec3<f32>(0.299, 0.587, 0.114);
    var luminance = dot(texel.xyz, luma);
       // 根据亮度自适应调节radius
    var adaptiveRadius = baseRadius * (0.5 + luminance * 2.0);  // 基础半径随亮度变化
       adaptiveRadius = clamp(adaptiveRadius, baseRadius * 0.2, baseRadius * 3.0);  // 限制范围
    var color = vec4<f32>(0.0);
    var totalWeight = 0.0;
    
    let FIXED_SAMPLES = 25u;  // 增加采样数量
    let PI2 = 6.28318530718;  // 2*PI的精确值
    let PI = 3.1415926;  // 2*PI的精确值

    for(var i = 0u; i < FIXED_SAMPLES; i = i + 1u) {
        var angle = (2.0 * PI * f32(i)) / f32(FIXED_SAMPLES);
      // 使用自适应半径替换原来的baseRadius
      var radius = sqrt(f32(i) / f32(FIXED_SAMPLES))/(1/adaptiveRadius);
        var offset = vec2<f32>(cos(angle), sin(angle)) * radius;
        var sampleDist = length(offset);
        var sampleColor = textureSample(tDiffuse, Sampler, input.uv + offset);
         // 物理散射计算
        var scatter = calculateColorDispersion(sampleDist);
        color += sampleColor * vec4<f32>(scatter,1.0) ;
        // color+=sampleColor;
        // totalWeight += scatter;
    }

    // output.color = vec4<f32>(color / max(totalWeight, 0.001), 1.0);
    output.color = vec4<f32>(color / f32(FIXED_SAMPLES));
    return output;
}
`;


const merge_frag = /*wgsl*/ `
fn energyConservingBloom(originalColor: vec4<f32>, bloomColor: vec4<f32>) -> vec4<f32> {
    // 确保总能量不超过入射光
    let totalEnergy = length(originalColor.rgb) + length(bloomColor.rgb);
    let maxEnergy = length(originalColor.rgb);
    
    var finalBloom = bloomColor;
    if(totalEnergy > maxEnergy) {
        finalBloom *= (maxEnergy / totalEnergy);
    }
    return originalColor + finalBloom*vec4( 1.0 );
}
@group(0) @binding(0) var originalTex: texture_2d<f32>;
@group(0) @binding(1) var brightTex: texture_2d<f32>;
@group(0) @binding(2) var texSampler: sampler;
@group(0) @binding(3) var<uniform> strength: f32;

@fragment
fn main(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
    let original = textureSample(originalTex, texSampler, uv);
    let bright = textureSample(brightTex, texSampler, uv);
    return original+strength *vec4( 1.0 )*bright ; // 简单相加，您可以根据需要调整混合方式
    // return energyConservingBloom(original, bright);
}`
const blend_frag = /*wgsl*/ `
@group(0) @binding(0) var tDiffuse: texture_2d<f32>;
@group(0) @binding(1) var texSampler: sampler;
@group(0) @binding(2) var<uniform> opacity: f32;

@fragment
fn main(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
    var texel =textureSample(tDiffuse, texSampler, uv);
    return opacity * texel;
}`
export { blur_frag, merge_frag, blend_frag };
